package com.zyw.codegen.api;

import com.zyw.codegen.Codegen;
import com.zyw.codegen.GenConfig;
import com.zyw.codegen.GenTable;
import com.zyw.mvc.servlet.BaseController;
import com.zyw.mvc.support.WrappedResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhangyw
 * @date 2017/3/16 16:32
 */
@RestController
@RequestMapping("${path.console.prefix}/codegen")
public class CodegenApiController extends BaseController {

    private Codegen codegen;

    public CodegenApiController() {
        this.codegen = new Codegen();
    }

    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public WrappedResult codegen(GenConfig config, GenTable table,
                                 @RequestParam(required = false, defaultValue = "dataSource") String dataSourceLookupKey) {

        if (config == null) return wrappedFaliedResult("no gen config");
        if (table == null) return wrappedFaliedResult("no gen table");

        codegen.setGenConfig(config);
        codegen.addTable(table);
        codegen.setDataSourceLookupKey(dataSourceLookupKey);
        codegen.generate();
        return wrappedSuccessResult("代码生成成功");
    }
}
